const Koa = require('koa')
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const render = require('koa-art-template')
const staticCache = require('koa-static-cache')
const path = require('path')

const router = require('./routes/')
const config = require('./config/')
const env = process.env.NODE_ENV
const app = new Koa()
render(app, {
  root: path.resolve(__dirname, 'views'),
  extname: '.art',
  debug: process.env.NODE_ENV !== 'production'
})

app.on('error', (err, ctx) => {
  console.error('server erro', err)
})
app.use(cors())
app.use(koaBody())
app.use(router.routes())
app.use(staticCache(path.join(__dirname, 'static'), {
  maxAge: 30 * 24 * 60 * 60
}))

const SERVER = app.listen(config[env].port)
SERVER.timeout = 0
